package cn.edu.sgu.www.mhxysy.service.fabao.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.fabao.FabaoLiangyiAttributeDTO;
import cn.edu.sgu.www.mhxysy.entity.fabao.*;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.fabao.*;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.fabao.FabaoLiangyiAttributeService;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class FabaoLiangyiAttributeServiceImpl implements FabaoLiangyiAttributeService {

    private final FabaoMapper fabaoMapper;
    private final FabaoSixiangTypeMapper fabaoSixiangTypeMapper;
    private final FabaoSixiangAttributeMapper fabaoSixiangAttributeMapper;
    private final FabaoLiangyiAttributeMapper fabaoLiangyiAttributeMapper;
    private final FabaoSixiangAttributeCategoryMapper fabaoSixiangAttributeCategoryMapper;

    @Autowired
    public FabaoLiangyiAttributeServiceImpl(
            FabaoMapper fabaoMapper,
            FabaoSixiangTypeMapper fabaoSixiangTypeMapper,
            FabaoSixiangAttributeMapper fabaoSixiangAttributeMapper,
            FabaoLiangyiAttributeMapper fabaoLiangyiAttributeMapper,
            FabaoSixiangAttributeCategoryMapper fabaoSixiangAttributeCategoryMapper) {
        this.fabaoMapper = fabaoMapper;
        this.fabaoSixiangTypeMapper = fabaoSixiangTypeMapper;
        this.fabaoSixiangAttributeMapper = fabaoSixiangAttributeMapper;
        this.fabaoLiangyiAttributeMapper = fabaoLiangyiAttributeMapper;
        this.fabaoSixiangAttributeCategoryMapper = fabaoSixiangAttributeCategoryMapper;
    }

    @Override
    public void insert(FabaoLiangyiAttributeDTO fabaoLiangyiAttributeDTO) {
        // 得到法宝ID
        String fabaoId = fabaoLiangyiAttributeDTO.getFabaoId();

        // 得到法宝的进阶次数
        QueryWrapper<FabaoLiangyiAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("fabao_id", fabaoId);

        long times = fabaoLiangyiAttributeMapper.selectCount(queryWrapper);

        if (times >= 3) {
            throw new GlobalException(ResponseCode.FORBIDDEN, "法宝进阶的次数已满。");
        }

        queryWrapper.eq("step", fabaoLiangyiAttributeDTO.getStep());

        long count = fabaoLiangyiAttributeMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new GlobalException(ResponseCode.FORBIDDEN, "法宝已经进阶过了。");
        }

        FabaoLiangyiAttribute fabaoLiangyiAttribute = toEntity(fabaoLiangyiAttributeDTO);

        // 1、添加法宝的两仪属性
        fabaoLiangyiAttribute.setId(fabaoLiangyiAttribute.uuid());

        fabaoLiangyiAttributeMapper.insert(fabaoLiangyiAttribute);

        times ++;

        // 进阶了3次，为法宝添加生成四象属性
        if (times == 3) {
            // 查询法宝的两仪属性中阳属性数量
            times = fabaoLiangyiAttributeMapper.selectCountByFabaoId(fabaoId);

            // 根据法宝两仪属性中阳属性的数量查询法宝四象
            Map<String, Object> columnMap = new HashMap<>();

            columnMap.put("count", times);

            List<FabaoSixiangType> sixiangTypes = fabaoSixiangTypeMapper.selectByMap(columnMap);

            // 得到查询结果，为法宝添加四象属性
            if (CollectionUtils.isNotEmpty(sixiangTypes)) {
                // 实际只查询到一条数据，直接得到集合第一条数据
                FabaoSixiangType sixiangType = sixiangTypes.get(0);
                Fabao fabao = fabaoMapper.selectById(fabaoId);

                // 通过法宝类型ID查询法宝四象
                UpdateWrapper<FabaoSixiangAttributeCategory> updateWrapper = new UpdateWrapper<>();

                updateWrapper.eq("type", sixiangType.getId());
                updateWrapper.eq("fabao_id", fabao.getCategoryId());

                FabaoSixiangAttributeCategory category = fabaoSixiangAttributeCategoryMapper.selectOne(updateWrapper);

                if (category != null) {
                    // 2、添加法宝的四象属性
                    FabaoSixiangAttribute fabaoSixiangAttribute = new FabaoSixiangAttribute();

                    fabaoSixiangAttribute.setId(fabaoSixiangAttribute.uuid());
                    fabaoSixiangAttribute.setFabaoId(fabaoId);
                    fabaoSixiangAttribute.setSixiangId(category.getId());

                    fabaoSixiangAttributeMapper.insert(fabaoSixiangAttribute);
                }
            }
        }
    }

    @Override
    public void deleteById(String id) {
        FabaoLiangyiAttribute fabaoLiangyiAttribute = fabaoLiangyiAttributeMapper.selectById(id);

        // 1、删除法宝的四象属性（如果存在）
        QueryWrapper<FabaoSixiangAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("fabao_id", fabaoLiangyiAttribute.getFabaoId());

        Long count = fabaoSixiangAttributeMapper.selectCount(queryWrapper);

        if (count > 0) {
            fabaoSixiangAttributeMapper.delete(queryWrapper);
        }

        // 2、删除两仪属性
        fabaoLiangyiAttributeMapper.deleteById(id);
    }

    @Override
    public void updateById(FabaoLiangyiAttributeDTO fabaoLiangyiAttributeDTO) {
        String id = fabaoLiangyiAttributeDTO.getId();

        if (StringUtils.isNullOrEmpty(id)) {
            throw new GlobalException(ResponseCode.BAD_REQUEST, CustomMessages.ID_NOT_NULL);
        }

        String fabaoId = fabaoLiangyiAttributeDTO.getFabaoId();

        // 查询法宝的两仪属性是否重复
        QueryWrapper<FabaoLiangyiAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.ne("id", StringUtils.isNotEmpty(id));

        queryWrapper.eq("fabao_id", fabaoId);
        queryWrapper.eq("step", fabaoLiangyiAttributeDTO.getStep());

        long total = fabaoLiangyiAttributeMapper.selectCount(queryWrapper);

        if (total > 0) {
            throw new GlobalException(ResponseCode.FORBIDDEN, "法宝已经进阶过了。");
        }

        // 1、修改法宝两仪属性
        FabaoLiangyiAttribute fabaoLiangyiAttribute = toEntity(fabaoLiangyiAttributeDTO);

        fabaoLiangyiAttributeMapper.updateById(fabaoLiangyiAttribute);

        // 查询法宝的四象属性
        UpdateWrapper<FabaoSixiangAttribute> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("fabao_id", fabaoId);

        total = fabaoSixiangAttributeMapper.selectCount(updateWrapper);

        if (total == 1) {
            // 查询法宝的两仪属性中阳属性数量
            int count = fabaoLiangyiAttributeMapper.selectCountByFabaoId(fabaoId);

            // 根据法宝两仪属性中阳属性的数量查询法宝四象
            Map<String, Object> columnMap = new HashMap<>();

            columnMap.put("count", count);

            List<FabaoSixiangType> sixiangTypes = fabaoSixiangTypeMapper.selectByMap(columnMap);

            // 得到查询结果，为法宝添加四象属性
            if (CollectionUtils.isNotEmpty(sixiangTypes)) {
                FabaoSixiangType sixiangType = sixiangTypes.get(0);

                Fabao fabao = fabaoMapper.selectById(fabaoId);

                // 查询法宝四象
                QueryWrapper<FabaoSixiangAttributeCategory> queryWrapper1 = new QueryWrapper<>();

                queryWrapper1.eq("type", sixiangType.getId());
                queryWrapper1.eq("fabao_id", fabao.getCategoryId());

                FabaoSixiangAttributeCategory fabaoSixiangAttributeCategory = fabaoSixiangAttributeCategoryMapper.selectOne(queryWrapper1);

                // 2、修改法宝的四象属性
                updateWrapper.set("sixiang_id", fabaoSixiangAttributeCategory.getId());
                updateWrapper.ne("sixiang_id", fabaoSixiangAttributeCategory.getId());

                fabaoSixiangAttributeMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        } else if (total > 1) {
            throw new GlobalException(ResponseCode.SERVER_ERROR, "数据异常，请联系管理员处理~");
        }
    }

    @Override
    public List<FabaoLiangyiAttribute> selectByFabaoId(String fabaoId) {
        QueryWrapper<FabaoLiangyiAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("fabao_id", fabaoId);

        return fabaoLiangyiAttributeMapper.selectList(queryWrapper);
    }

    private FabaoLiangyiAttribute toEntity(FabaoLiangyiAttributeDTO fabaoLiangyiAttributeDTO) {
        FabaoLiangyiAttribute fabaoLiangyiAttribute = new FabaoLiangyiAttribute();

        fabaoLiangyiAttribute.setId(fabaoLiangyiAttributeDTO.getId());
        fabaoLiangyiAttribute.setStep(fabaoLiangyiAttributeDTO.getStep());
        fabaoLiangyiAttribute.setFabaoId(fabaoLiangyiAttributeDTO.getFabaoId());
        fabaoLiangyiAttribute.setValueId(fabaoLiangyiAttributeDTO.getValueId());
        fabaoLiangyiAttribute.setLiangyiId(fabaoLiangyiAttributeDTO.getLiangyiId());

        return fabaoLiangyiAttribute;
    }

}